﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Transactions;

namespace WebOS.Core.ORM.Infrastructure
{
    public class UnitOfWork : IUnitOfWork
    {
        private readonly IDatabaseFactory databaseFactory;
        private DbContext dataContext;

        public UnitOfWork(IDatabaseFactory databaseFactory)
        {
            this.databaseFactory = databaseFactory;
        }

        protected DbContext DataContext
        {
            get { return dataContext ?? (dataContext = databaseFactory.Get()); }
        }

        public int Commit()
        {
            return DataContext.SaveChanges();
        }
        /// <summary>
        /// 分布式提交
        /// </summary>
        public void Commit2()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                //int a = context1.SaveChanges();
                //int b = context2.SaveChanges();
                scope.Complete();
            }
        }
    }
}
